package com.qianfeng.zhangwei.day30_bdmap.location;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.qianfeng.zhangwei.day30_bdmap.R;

/**
 *
 */
public class LocationActivity extends AppCompatActivity {

    LocationClient locationClient;
    private BDLocationListener bdLocationListener = new AppBdLocationListener();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        initLocation();

    }

    /**
     * 初始化定位功能
     */
    private void initLocation() {
        //第一步实例化定位核心类 设置定位参数
        locationClient = new LocationClient(this, getLocOption());
        //第二步注册监听
        locationClient.registerLocationListener(bdLocationListener);
        //第三步开始定位
        locationClient.start();

    }

    /**
     * 配置定位参数
     *
     * @return
     */
    private LocationClientOption getLocOption() {
        LocationClientOption locationClientOption = new LocationClientOption();
        //指定定位模式
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //设置坐标的系的类型(必须)
        locationClientOption.setCoorType("bd09ll");
        locationClientOption.setScanSpan(5000);
        locationClientOption.setIsNeedAddress(true);
        locationClientOption.setOpenGps(true);

        return locationClientOption;
    }

    private class AppBdLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //回调方法执行频率根据scanspan 设置的参数有关
            double lat = bdLocation.getLatitude();
            double lngt = bdLocation.getLongitude();
            String address = bdLocation.getAddrStr();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //第四步  根据业务需求来停止定位
        locationClient.stop();
    }
}
